#include <Engine/StdAfx.h>
#include <Engine/RenderObjects/DebugHud.h>
#include <Engine/GlobalEnv.h>
#include <Engine/IRenderer.h>
#include <Engine/IEngine.h>
#include <Engine/IFont.h>
#include <Engine/IInputLayout.h>
#include <Engine/Renderer/IRenderState.h>
#include <Engine/GlobalEnv.h>
#include <Engine/IEngine.h>
#include <Engine/Math/Math.h>

using namespace fastbird;

//----------------------------------------------------------------------------
DebugHud::DebugHud()
{
	mTexts.reserve(100);
	mObjectConstants.gWorldViewProj = MakeOrthogonalMatrix(0, 0, 
		(float)gEnv->pEngine->GetRenderer()->GetWidth(),
		(float)gEnv->pEngine->GetRenderer()->GetHeight(),
		0.f, 1.0f); 
}

//----------------------------------------------------------------------------
DebugHud::~DebugHud()
{
}

//----------------------------------------------------------------------------
void DebugHud::QueueText(const Vec2I& pos, WCHAR* text, 
	const Vec3& color, float size)
{
	DebugText request;
	request.mPos = pos;
	request.mText = text;
	request.mColor = color;
	request.mSize = size;
	mTexts.push_back(request);
}

//----------------------------------------------------------------------------
void DebugHud::Render()
{	
	// object constant buffer
	IFont* pFont = gEnv->pEngine->GetFont();
	gEnv->pEngine->GetRenderer()->UpdateObjectConstantsBuffer(&mObjectConstants);
	pFont->PrepareRenderResources();
	mRasterizerState->Bind();
	mBlendState->Bind();

	auto it = mTexts.begin(), itEnd = mTexts.end();
	for ( ; it!=itEnd; it++)
	{
		pFont->Write((float)it->mPos.x, (float)it->mPos.y, 0.5f, it->mColor.Get4Byte(), 
			(const char*)it->mText.c_str(), 0, FONT_ALIGN_LEFT);
	}
	mTexts.clear();
}

//----------------------------------------------------------------------------
void DebugHud::PreRender()
{
}

//----------------------------------------------------------------------------
void DebugHud::PostRender()
{
}